/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agilelang.generator.cakephp;

import java.io.File;
import java.util.List;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IReport;
import org.agilelang.object.Project;
import org.agilelang.object.Report;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.Reference;
import org.agilelang.object.attributes.ViewReport;

/**
 *
 * @author Roderick
 */
public class GReport extends Generator implements IReport {

    protected Project project;
    List<Report> reports;

    GReport(String path_app, Project project, List<Report> reports) {
        this.template = "bootstrap";
        setTemplate2("cakephp");
        this.path = path_app;
        this.controllers = project.controllers;
        this.models = project.models;
        this.menus = project.menus;
        this.appName = project.application.name;
        this.reports = reports;
    }

    @Override
    public void process() {
        int t = this.reports.size();
        String [] abc = {"A","B","C","D","E","F","G","H","","","","","","","","","",""};
        String functions = "";
        for (int i = 0; i < t; i++) {
            Report report = reports.get(i);
            this.createDirectory(path + File.separator + "view/" + report.name.toLowerCase());
            List<Attribute> attrs = this.getModelByName(report.model).attributes;
            List<Reference> references = this.getModelByName(report.model).references;

            List<ViewReport> viewreports = report.reports;
            for (int x = 0; x < viewreports.size(); x++) {
                ViewReport viewreport = viewreports.get(x);
                String th_attrs = "", td_attrs = "",th_attrs_excel = "", td_attrs_excel = "";
                String[] attr_str = viewreport.attributes.split(",");
                int tt = attr_str.length;
                for (int j = 0; j < tt; j++) {
                    Attribute attr = search_attr(attr_str[j].trim(), attrs);
                    if (attr.name.endsWith("_id")) {
                        int tr = references.size();
                        for (int jj = 0; jj < tr; jj++) {
                            Reference reference = references.get(jj);
                            if (attr.name.equals(reference.foreign_key)) {
                                td_attrs = td_attrs + "\t\t\t$pdf->Cell(30,5,utf8_decode($" + report.model.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "']),1);\r\n";
                                td_attrs_excel += "\t\t\t$objPHPExcel->getActiveSheet()->SetCellValue('"+abc[j+1]+"'.$count, $" + report.model.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "']);\n";
                                break;
                            }
                        }
                    } else {
                        if (attr.type.equals("boolean")) {
                            td_attrs += "\t\t\t$pdf->Cell(30,5,utf8_decode($" + report.model.toLowerCase() + "['" + report.model + "']['" + attr.name + "'])== '1' ? 'Si' :'No',1);\r\n";
                            td_attrs_excel += "\t\t\t$objPHPExcel->getActiveSheet()->SetCellValue('"+abc[j+1]+"'.$count, $" + report.model.toLowerCase() + "['" + report.model + "']['" + attr.name + "'])== '1' ? 'Si' :'No');\n";
                        } else {
                            td_attrs += "\t\t\t$pdf->Cell(30,5,utf8_decode($" + report.model.toLowerCase() + "['" + report.model + "']['" + attr.name + "']),1);\r\n";
                            td_attrs_excel += "\t\t\t$objPHPExcel->getActiveSheet()->SetCellValue('"+abc[j+1]+"'.$count, $" + report.model.toLowerCase() + "['" + report.model + "']['" + attr.name + "']);\n";

                        }
                    }



                    th_attrs += "\t\t$pdf->Cell(30,5,utf8_decode('" + attr.caption + "'),1);\n";
                    th_attrs_excel += "\t\t$objPHPExcel->getActiveSheet()->SetCellValue('"+abc[j+1]+"2', '" + attr.caption + "');\n";
                }
                String file = readStreamTpl("r_function_pdf.tpl");
                file = file.replace("//name_function//", viewreport.name);
                file = file.replace("//nameCaption//", viewreport.caption);
                file = file.replace("//Model//", report.model);
                file = file.replace("//model//", report.model.toLowerCase());
                file = file.replace("//models//", this.setPluralize(report.model).toLowerCase());
                file = file.replace("//th_attrs//", th_attrs);
                file = file.replace("//td_attrs//", td_attrs);
                functions += file;
                
                file = readStreamTpl("r_function_excel.tpl");
                file = file.replace("//name_function//", viewreport.name);
                file = file.replace("//nameCaption//", viewreport.caption);
                file = file.replace("//Model//", report.model);
                file = file.replace("//model//", report.model.toLowerCase());
                file = file.replace("//models//", this.setPluralize(report.model).toLowerCase());
                file = file.replace("//th_attrs//", th_attrs_excel);
                file = file.replace("//td_attrs//", td_attrs_excel);
                
                functions += file;
            }



            String file = readStreamTpl("r_controller.php.tpl");
            file = file.replace("//Model//", report.model);
            file = file.replace("//nameClass//", report.name);
            file = file.replace("//functions//", functions);
            createfile("controller/" + report.name + "Controller.php", path, file);

            index(report);
        }

    }

    public void index(Report report) {
        String file = readStreamTpl("r_index.php.tpl");
        file = file.replace("//nameClass//", report.name);
        String link ="";
        List<ViewReport> viewreports = report.reports;
        for (int x = 0; x < viewreports.size(); x++) {
            ViewReport viewreport = viewreports.get(x);
            link +="<tr>"
                    + "<td>"+viewreport.caption+"</td>"
                    + "<td><?php echo $this->Html->link('<i class=\"icon-file icon-white\"></i>' . __('PDF'), array('action' => '"+viewreport.name+"_pdf'), array('class' => 'btn btn-primary', 'type' => 'button', 'escape' => FALSE)); ?></td>"
                    + "<td><?php echo $this->Html->link('<i class=\"icon-file icon-white\"></i>' . __('Excel'), array('action' => '"+viewreport.name+"_excel'), array('class' => 'btn btn-primary', 'type' => 'button', 'escape' => FALSE)); ?></td>"
                    + "</tr>";
        }
        file = file.replace("//nameCaption//", report.caption);
        file = file.replace("//link//", link);
        createfile("view/" + report.name.toLowerCase() + "/index.ctp", path, file);
    }

    public Attribute search_attr(String name, List<Attribute> list) {
        int tt = list.size();
        for (int j = 0; j < tt; j++) {
            Attribute attr = list.get(j);
            if (attr.name.equals(name)) {
                return attr;
            }
        }
        return null;
    }
}
